//
//  UIViewController+HUD.m
//  YBInsurance
//
//  Created by S2 on 12/25/15.
//  Copyright © 2015 YB. All rights reserved.
//

#import "UIViewController+HUD.h"
#import <MBProgressHUD/MBProgressHUD.h>

@implementation UIViewController (HUD)

- (void)showHUDWith:(NSString *)text showView:(UIView *)showView{
    
    dispatch_async(dispatch_get_main_queue(), ^{
    
        [self hideHUD:showView];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:showView animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = text;
        hud.margin = 10.f;
        hud.label.font = [UIFont systemFontOfSize:13];
        hud.label.font = [UIFont systemFontOfSize:13];
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:1.5];
    });
}

- (void)showLoadingHUD:(NSString *)text showView:(UIView *)showView{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self hideHUD:showView];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:showView animated:YES];
        hud.mode = MBProgressHUDModeIndeterminate;
        hud.label.text = text;
        hud.margin = 10.f;
        hud.label.font = [UIFont systemFontOfSize:13];
        hud.label.font = [UIFont systemFontOfSize:13];
        hud.removeFromSuperViewOnHide = YES;
    });
}

- (void)delay:(CGFloat)duration action:(CallBack)callBack {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        callBack();
    });
}

#pragma mark - Private 
/**
 *  @brief  隐藏页面上的HUD
 */

- (void)hideHUD:(UIView *)view{
    
    if ([MBProgressHUD HUDForView:view]) {
            [MBProgressHUD hideHUDForView:view animated:YES];
        }
}
@end
